package com.mdd.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mdd.admin.service.IProposalService;
import com.mdd.admin.validate.commons.PageValidate;
import com.mdd.admin.vo.ProposalVo;
import com.mdd.common.core.PageResult;
import com.mdd.common.entity.Proposal;
import com.mdd.common.entity.user.User;
import com.mdd.common.mapper.ProposalMapper;
import com.mdd.common.mapper.user.UserMapper;
import com.mdd.common.util.TimeUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 建议反馈服务类
 * @author shenjian
 * @createTime 2023年03月04日 11:32:00
 */
@Service
public class ProposalServiceImpl implements IProposalService {

    @Resource
    ProposalMapper proposalMapper;
    @Resource
    UserMapper userMapper;

    @Override
    public PageResult<ProposalVo> list(PageValidate pageValidate) {
        Integer pageNo = pageValidate.getPageNo();
        Integer pageSize = pageValidate.getPageSize();

        QueryWrapper<Proposal> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("create_time");
        Page<Proposal> iPage = proposalMapper.selectPage(new Page<>(pageNo, pageSize), queryWrapper);
        List<ProposalVo> list = new ArrayList<>();
        if (!iPage.getRecords().isEmpty()) {
            List<Integer> userIds = iPage.getRecords().stream()
                    .map(Proposal::getUserId)
                    .collect(Collectors.toList());
            List<User> userList = userMapper.selectBatchIds(userIds);
            Map<Integer, List<User>> userMap = new HashMap<>();
            if (CollectionUtils.isNotEmpty(userList)) {
                userMap = userList.stream().collect(Collectors.groupingBy(User::getId));
            }
            for (Proposal proposal : iPage.getRecords()) {
                ProposalVo vo = new ProposalVo();
                BeanUtils.copyProperties(proposal, vo);
                vo.setProposalId(proposal.getId());
                List<User> users = userMap.get(proposal.getUserId());
                if (CollectionUtils.isNotEmpty(users)) {
                    User user = users.get(0);
                    vo.setUserId(user.getSn());
                    vo.setUserName(user.getUsername());
                    vo.setPhone(user.getMobile());
                }
                list.add(vo);
            }
        }
        return PageResult.iPageHandle(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), list);
    }

    @Override
    public void adopt(Integer id, String reply) {
        Proposal proposal = proposalMapper.selectById(id);
        proposal.setStatus(1);
        proposal.setReply(reply);
        proposal.setUpdateTime(TimeUtils.timestamp());
        proposalMapper.updateById(proposal);
    }
}
